package servlet.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDao;
import dbutil.DBHelper;
import net.sf.json.JSONObject;

public class DelUserServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public DelUserServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		JSONObject jsonObject = new JSONObject();
		Connection conn = null;
		UserDao userDao = null;
		
		try {
			conn = DBHelper.getConnection();
			conn.setAutoCommit(false);
			userDao = new UserDao();
			
			if(userDao.delUserByName(request.getParameter("userName"))) {
				jsonObject.put("retCode", "1");
			} else {// 删除失败
				jsonObject.put("retCode", "0");
				jsonObject.put("desc", "删除失败，请检查是否存在许可请求");
			}
			conn.setAutoCommit(true);
		} catch (SQLException e) {
			e.printStackTrace();
			jsonObject.put("retCode", "0");
			jsonObject.put("desc", "DelUserServlet Exception");
		} finally {
			out.print(jsonObject.toString());
			out.close();
		}
	}

	public void init() throws ServletException {
	}

}
